public class LinkedList<E> {

    private class Node{
        public E e;
        public Node next;

        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }

        public Node(E e){
            this(e,null);
        }
        public Node(Node next){
            this(null,next);
        }

        public Node(){
            this(null,null);
        }

        @Override
        public String toString(){
            return e.toString();
        }
    }

    private Node dummyHead;
    private int size;

    public LinkedList(){
        dummyHead = new Node(null,null);
        size = 0;
    }

    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }


    //在链表的指定位置添加元素,关键在于找到添加位置的前一个元素
    public void add(int index,E e){
        if(index < 0 || index > size){
            throw new IllegalArgumentException("add error,Illegal index");
        }

        //找到指定位置的前一个元素,循环让pre一直移动到需要的那个节点
        Node pre = dummyHead;
        for(int i =0 ;i<index;i++){
            pre = pre.next;
        }
        //将插入元素next指向pre的next
        Node node = new Node(e);
        node.next = pre.next;
        //将pre的的next指向插入元素
        pre.next = node;

        //也可简写
//        pre.next = new Node(e,pre.next);

        size ++;
    }

    //在链表头添加新的元素
    public void addFirst(E e){
        add(0,e);
    }

    public void addLast(E e){
        add(size,e);
    }

    //获取链表指定位置上的元素
    public E get(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("get error,Illegal index");
        }
        Node cur = dummyHead.next;
        for(int i=0;i<size;i++){
            cur = cur.next;
        }
        return cur.e;
    }

    //获取链表的第一个元素
    public E getFirst(){
       return get(0);
    }

    //获取链表的最后一个元素
    public E getLast(){
        return get(size - 1);
    }

    //修改某位置的元素
    public void set(int index,E e){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("set error,Illegal index");
        }
        Node cur = dummyHead.next;
        for(int i=0;i<size;i++){
            cur = cur.next;
        }
        cur.e = e;
    }

    //查找链表中是否含有某元素e
    public boolean contains(E e){
        Node cur = dummyHead.next;
        while(cur != null){
            if(e.equals(cur.e)){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //从链表中删除某位置的元素
    public E remove(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("remove error,Illegal index");
        }
        Node pre = dummyHead;
        //找到删除节点的前一个节点
        for(int i = 0;i<index;i++){
            pre = pre.next;
        }
        //将删除节点的前一个节点的next指向删除节点的next
        Node delNode = pre.next;
        pre.next = delNode.next;
        delNode.next = null;
        size --;
        return delNode.e;
    }

    //从链表中删除第一个元素
    public E removeFirst(){
        return remove(0);
    }
    //从链表中删除最后一个元素
    public E removeLast(){
        return remove(size - 1);
    }
    //移除某个元素
    public void removeElement(E e){
        Node pre = dummyHead;
        //找到删除节点的前一个节点
        while(pre.next != null){
            if(e.equals(pre.next.e)){
                //此时pre就是删除节点的前一个节点
                Node delNode = pre.next;
                pre.next = delNode.next;
                delNode.next = null;
                size --;
            }else{
                pre = pre.next;
            }
        }
    }
    @Override
    public String toString(){
        StringBuffer sb = new StringBuffer();
        Node cur = dummyHead.next;
        while(cur != null){
            sb.append(cur +"->");
            cur = cur.next;
        }
        sb.append("NULL");
        return sb.toString();
    }

}
